package com.kantboot.api.yunzhanghu.service.impl;

import com.kantboot.api.yunzhanghu.service.IApiYunzhanghuPayService;
import com.kantboot.api.yunzhanghu.setting.ApiYunzhanghuSetting;
import com.kantboot.util.common.exception.BaseException;
import com.yunzhanghu.sdk.base.YzhRequest;
import com.yunzhanghu.sdk.base.YzhResponse;
import com.yunzhanghu.sdk.payment.PaymentClient;
import com.yunzhanghu.sdk.payment.domain.CreateBankpayOrderRequest;
import com.yunzhanghu.sdk.payment.domain.CreateBankpayOrderResponse;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

@Log4j2
@Service
public class ApiYunzhanghuPayServiceImpl implements IApiYunzhanghuPayService {

    @Resource
    private ApiYunzhanghuSetting yunzhanghuSetting;

    @Override
    public void payToBankCard(CreateBankpayOrderRequest param) {
        PaymentClient paymentClient = yunzhanghuSetting.getPaymentClient();

        // 设置平台dealerId和brokerId
        param.setDealerId(paymentClient.getYzhConfig().getDealerId());
        param.setBrokerId(paymentClient.getYzhConfig().getBrokerId());

        YzhResponse<CreateBankpayOrderResponse> response = null;
        try {
            response = paymentClient.createBankpayOrder(YzhRequest.build("requestId", param));
            if (response.isSuccess()) {
                // 操作成功
                CreateBankpayOrderResponse data = response.getData();
//                System.out.println("操作成功 " + data);
                log.info("操作成功 {}", data);
            } else {
                // 失败返回
                log.error("失败返回 code：{} message：{} requestId：{}", response.getCode(), response.getMessage(), response.getRequestId());
//                System.out.println("失败返回 code：" + response.getCode() + " message：" + response.getMessage() + " requestId：" + response.getRequestId());
                throw BaseException.of("yunzhanghuError"+response.getCode(), response.getMessage());
            }
        }
        catch (BaseException e) {
            throw e;
        }
        catch (Exception e) {
            // 发生异常
            e.printStackTrace();
        }

    }



}
